home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Moscow ML 1.31 / source code / mosml / src / test / teste.sml < prev    next >
Encoding:
Text File  |  1996-07-03  |  1.9 KB  |  66 lines  |  [TEXT/R*ch]

  1. (* Floating-point exceptions.  Works for 64-bit arithmetic on PCs *)
  2.  
  3. val MAXDOUBLE = 8.98846567431157E307;
  4. val MINDOUBLE = 4.94065645841246544E~324
  5. val pi = 3.14159265358979323846;
  6. val eps = 1E~15;
  7. infix seq
  8. fun e1 seq e2 = e2;
  9.  
  10. fun check1 (opr, a, r) =
  11.     let val res = opr a
  12.     in
  13.     if r = 0.0 andalso abs res <= eps orelse abs (res/r - 1.0) <= eps
  14.     then "OK" else "WRONG"
  15.     end;
  16.  
  17. check1(abs, 1.9E~212, 1.9E~212);
  18. check1(abs, ~1.9E~212, 1.9E~212);
  19. check1(~, 1.9E~212, ~1.9E~212);
  20. check1(~, ~1.9E~212, 1.9E~212);
  21. check1(real, 515, 515.0);
  22. check1(real, ~515, ~515.0);
  23. check1(sqrt, 64.0, 8.0);
  24. check1(sin, 0.0, 0.0);
  25. check1(sin, pi/2.0, 1.0);
  26. check1(sin, pi, 0.0);
  27. check1(sin, 3.0*pi/2.0, ~1.0);
  28. check1(cos, 0.0, 1.0);
  29. check1(cos, pi/2.0, 0.0);
  30. check1(cos, pi, ~1.0);
  31. check1(cos, 3.0*pi/2.0, 0.0);
  32.  
  33. fun check2 (opr, a1, a2, r) =
  34.     let val res = opr(a1, a2)
  35.     in
  36.     if r = 0.0 andalso abs res <= eps orelse abs (res/r - 1.0) <= eps
  37.     then "OK" else "WRONG"
  38.     end;
  39.  
  40. check2(op+, 1.6, 2.3, 3.9);
  41. check2(op+, ~1E123, 2E124, 190E122);
  42. check2(op-, 16.0, 28.0, ~12.0);
  43. check2(op-, ~8E23, 4E24, ~480E22);
  44. check2(op*, 1E100, 1.234E8, 1.234E108);
  45. check2(op*, 1E~100, 1.234E~8, 1.234E~108);
  46. check2(op/, 0E500, 1.0, 0.0);
  47. check2(op/, 1.0, ~1E~80, ~1E80);
  48.  
  49. (1.0/0.0    seq "WRONG") handle Quot => "OK";
  50. ((~1.0)/0.0 seq "WRONG") handle Quot => "OK";
  51. (1.0/(~0.0) seq "WRONG") handle Quot => "OK";
  52.  
  53. (MAXDOUBLE + 1E300       seq "WRONG") handle Sum => "OK";
  54. (~MAXDOUBLE - 1E300      seq "WRONG") handle Diff => "OK";
  55. (MAXDOUBLE * 1.000000001 seq "WRONG") handle Prod => "OK";
  56. (1.0 / MINDOUBLE         seq "WRONG") handle Quot => "OK";
  57.  
  58. val maxexp = ln MAXDOUBLE * 0.999999999999;
  59. (exp maxexp seq "OK") handle Exp => "WRONG";
  60. (exp (maxexp + 0.000001) seq "WRONG") handle Exp => "OK";
  61.  
  62. if MAXDOUBLE + ~MAXDOUBLE = 0.0 then "OK" else "WRONG";
  63.  
  64. fun f x = let val x2 = x / 2.0;
  65.       in MINDOUBLE/x2 + f x2 handle Div => 1.17 end;
  66.